/*
 * @Date: 2023-03-03 21:00:50
 * @LastEditors: Zireael 1193875164@qq.com
 * @LastEditTime: 2023-04-11 16:08:31
 * @FilePath: \A19\frontend\src\utils\decorators.ts
 */
const GetStreamDecorator:MethodDecorator = ()=>{

}

//廃棄
const ErrorDecorator:MethodDecorator = (target:Object,propertyKey: string | symbol,descriptor:PropertyDescriptor)=>{
    // console.log(target);
    // console.log(propertyKey);
    // console.log(descriptor);
    const sourceMethod = descriptor.value
    descriptor.value = async function name(...args:any) {
        try {
           return await sourceMethod.apply(this,args)
        } catch (error) {
            if(error instanceof Error){
                console.error(error);
                console.log(error);
                if(error.message === 'Permission denied' || error.message === 'The request is not allowed by the user agent or the platform in the current context.'){
                    ElMessage.error('没有权限，请重试')
                }
                else if(error.message === 'Failed to allocate videosource' || error.message === 'Requested device not found'){
                    ElMessage.error('摄像头不可用')
                }
                // else if(error.message === 'Could not start video source'){
                    
                // }
                else{
                    ElMessage.error(error.message)
                }
                throw error
            }
        }
    }
}

class VideoStreamGetter {
  /**
   * firefox
   * NotReadableError//摄像机占用 | denied by system
   * NotAllowedError//denied
   * OverconstrainedError//over constrained
   * NotFoundError//no device
   *
   * chrome
   * NotReadableError//摄像机占用
   * NotAllowedError//denied by system | denied
   * OverconstrainedError//over constrained
   * NotFoundError//no device
   */
  // @ErrorDecorator
  static async getCameraStream(constraints: MediaStreamConstraints) {
    try {
      const stream = await navigator.mediaDevices.getUserMedia(constraints);
    //   console.log(stream);
    //   VideoStreamGetter.getVideoInfos(stream);
      return stream;
    } catch (err) {
      const error = err as Error;
      console.log(error.name);
      const type = VideoStreamGetter.getBrowserType()
      if(type === 'firefox'){
        switch (error.name) {
            case "NotReadableError":
                ElMessage.error('设备被占用 或 系统阻止浏览器访问设备');
              break;
            case "NotAllowedError":
                ElMessage.error('访问设备被拒绝');
              break;
            case "NotFoundError":
                ElMessage.error('找不到设备');
              break;
            case "OverconstrainedError":
                ElMessage.error('设备不支持，请更换设备');
              break;
            default:
              ElMessage.error(error.message);
              break;
          }
      }
      else{
        switch (error.name) {
            case "NotReadableError":
                ElMessage.error('设备被占用');
              break;
            case "NotAllowedError":
                ElMessage.error('访问设备被系统或用户拒绝');
              break;
            case "NotFoundError":
                ElMessage.error('找不到设备');
              break;
            case "OverconstrainedError":
                ElMessage.error('设备不支持，请更换设备');
              break;
            default:
              ElMessage.error(error.message);
              break;
          }
      }
      
      throw error;
    }
  }

  /**
   * firefox
   *
   *
   * chrome
   *
   */
  // @ErrorDecorator
  static async getScreenStream(constraints: MediaStreamConstraints) {
    try {
      const stream = await navigator.mediaDevices.getDisplayMedia(constraints);
      VideoStreamGetter.getVideoInfos(stream);
      return stream;
    } catch (error) {
      if (error instanceof Error) {
        ElMessage.error("请勿阻止浏览器共享屏幕");
        throw error;
      }
    }
  }

  static getVideoInfos(stream: MediaStream) {
    const tracks = stream.getVideoTracks();
    tracks.forEach((track) => {
      console.log(track.getSettings());
    });
  }

  static getBrowserType() {
    let ua = navigator.userAgent.toLocaleLowerCase();
    let browserType = null;
    if (ua.match(/msie/) != null || ua.match(/trident/) != null) {
      browserType = "IE";
    } else if (ua.match(/firefox/) != null) {
      browserType = "firefox";
    } else if (ua.match(/ucbrowser/) != null) {
      browserType = "UC";
    } else if (ua.match(/opera/) != null || ua.match(/opr/) != null) {
      browserType = "opera";
    } else if (ua.match(/bidubrowser/) != null) {
      browserType = "baidu";
    } else if (ua.match(/metasr/) != null) {
      browserType = "sougou";
    } else if (
      ua.match(/tencenttraveler/) != null ||
      ua.match(/qqbrowse/) != null
    ) {
      browserType = "QQ";
    } else if (ua.match(/maxthon/) != null) {
      browserType = "maxthon";
    } else if (ua.match(/chrome/) != null) {
      browserType = "chrome";
    } else if (ua.match(/safari/) != null) {
      browserType = "Safari";
    } else {
      browserType = "others";
    }
    return browserType;
  }
}

export {
    VideoStreamGetter
}